package com.ruoyi.numericalselection.service.impl;

import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import ch.qos.logback.core.joran.util.beans.BeanUtil;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.numericalselection.config.RediskyeConifg;
import com.ruoyi.numericalselection.domain.GmAttribute;
import com.ruoyi.numericalselection.domain.dao.GmAttributeDao;
import com.ruoyi.numericalselection.mapper.GmAttributeMapper;
import com.ruoyi.numericalselection.service.IGmAttributeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * 属性Service业务层处理
 *
 * @author ruoyi
 * @date 2024-06-02
 */
@Service
public class GmAttributeServiceImpl implements IGmAttributeService {
    @Autowired
    private GmAttributeMapper gmAttributeMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查询属性
     *
     * @param id 属性主键
     * @return 属性
     */
    @Override
    public GmAttribute selectGmAttributeById(Long id) {
        return gmAttributeMapper.selectGmAttributeById(id);
    }

    /**
     * 查询属性列表
     *
     * @param gmAttribute 属性
     * @return 属性
     */
    @Override
    public List<GmAttribute> selectGmAttributeList(GmAttribute gmAttribute) {
        return gmAttributeMapper.selectGmAttributeList(gmAttribute);
    }

    /**
     * 新增属性
     *
     * @param gmAttribute 属性
     * @return 结果
     */
    @Override
    public int insertGmAttribute(GmAttribute gmAttribute) {
        return gmAttributeMapper.insertGmAttribute(gmAttribute);
    }

    /**
     * 修改属性
     *
     * @param gmAttribute 属性
     * @return 结果
     */
    @Override
    public int updateGmAttribute(GmAttribute gmAttribute) {
        redisTemplate.delete(RediskyeConifg.GM_KEY_ATTR + gmAttribute.getId());
        return gmAttributeMapper.updateGmAttribute(gmAttribute);
    }

    /**
     * 批量删除属性
     *
     * @param ids 需要删除的属性主键
     * @return 结果
     */
    @Override
    public int deleteGmAttributeByIds(Long[] ids) {
        List<Long> arrt = Arrays.asList(ids);
        for (Long aLong : arrt) {
            redisTemplate.delete(RediskyeConifg.GM_KEY_ATTR + aLong);
        }
        return gmAttributeMapper.deleteGmAttributeByIds(ids);
    }

    /**
     * 删除属性信息
     *
     * @param id 属性主键
     * @return 结果
     */
    @Override
    public int deleteGmAttributeById(Long id) {

        redisTemplate.delete(RediskyeConifg.GM_KEY_ATTR +id);
        return gmAttributeMapper.deleteGmAttributeById(id);
    }

    @Override
    public List<GmAttributeDao> getAttribute(Long id) {
        List<GmAttributeDao> bookTree = (List<GmAttributeDao>) redisTemplate.opsForValue().get(RediskyeConifg.GM_KEY_ATTR + id);
        if (bookTree == null || bookTree.isEmpty()) {
            List<GmAttribute> attributes = gmAttributeMapper.getAttribute(id);
            List<GmAttributeDao> attributeDaoList = attributes.stream().map(itme -> {
                GmAttributeDao gmAttributeDao = new GmAttributeDao();
                BeanUtils.copyProperties(itme, gmAttributeDao);
                return gmAttributeDao;
            }).collect(Collectors.toList());
            /**
             * 获取顶级父节点
             */
            List<GmAttributeDao> pidAttt = attributeDaoList.stream().filter(itme -> itme != null && itme.getPid().equals(0L)).collect(Collectors.toList());
            if (pidAttt == null) {
                return new ArrayList<>();
            }
            bookTree = getBookTree(pidAttt);
            redisTemplate.opsForValue().set(RediskyeConifg.GM_KEY_ATTR + id, bookTree, 1, TimeUnit.DAYS);
        }

        return bookTree;
    }

    public List<GmAttributeDao> getBookTree(List<GmAttributeDao> pidAttt) {
        List<GmAttributeDao> collect = pidAttt.stream()
                .filter(itme -> itme != null && itme.getPid().equals(0L))
                .sorted(Comparator.comparing(GmAttributeDao::getSort)) // 添加排序逻辑
                .map(m -> {
                    m.setChildren(getChildren(m, pidAttt));
                    return m;
                })
                .collect(Collectors.toList());
        return collect;
    }


    public List<GmAttributeDao> getChildren(GmAttribute root, List<GmAttributeDao> pidAttt) {
        List<GmAttributeDao> children = pidAttt.stream().filter(t -> {
            return Objects.equals(t.getPid(), root.getId());
        }).sorted(Comparator.comparing(GmAttributeDao::getSort)).map(
                m -> {
                    m.setChildren(getChildren(m, pidAttt));
                    return m;
                }
        ).collect(Collectors.toList());
        return children;
    }






}
